﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class DrawJigCurve
	{
		public DrawJigCurve(ExternalCommandData cmdData)
		{
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
		}

		public DrawJigCurve(UIDocument uiDoc)
		{
			this.m_uiDoc = uiDoc;
		}

		public Result DrawJigCurveLine(ref List<Curve> lstCurves, ref XYZ ptLast, string strPromptStart, string strPromptEnd)
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			bool flag = ptLast != null;
			XYZ xyz;
			if (flag)
			{
				xyz = ptLast;
				xlineJig.StartPoint = xyz;
			}
			else
			{
				xyz = xlineJig.PickStartPoint(strPromptStart);
				bool flag2 = xyz == null;
				if (flag2)
				{
					xlineJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			XYZ xyz2 = xlineJig.PickEndPoint(xyz, strPromptEnd);
			bool flag3 = xyz2 == null;
			Result result;
			if (flag3)
			{
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			else
			{
				lstCurves = xlineJig.CreateCurves();
				ptLast = xyz2;
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		public Result DrawJigCurveLine(ref Line line, ref XYZ ptLast, string strPromptStart, string strPromptEnd)
		{
			List<Curve> list = new List<Curve>();
			Result result = this.DrawJigCurveLine(ref list, ref ptLast, strPromptStart, strPromptEnd);
			bool flag = list.Count > 0;
			if (flag)
			{
				line = (list[0] as Line);
			}
			return result;
		}

		public Result DrawJigCurveLineXDirection(ref Line line, ref XYZ ptLast, string strPromptStart, string strPromptEnd)
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			bool flag = ptLast != null;
			XYZ xyz;
			if (flag)
			{
				xyz = ptLast;
				xlineJig.StartPoint = xyz;
			}
			else
			{
				xyz = xlineJig.PickStartPoint(strPromptStart);
				bool flag2 = xyz == null;
				if (flag2)
				{
					xlineJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			xlineJig.ResetPoint = XUiJigBase.EResetPoint.eByAngle;
			xlineJig.LineAngle = 0.0;
			XYZ xyz2 = xlineJig.PickEndPoint(xyz, strPromptEnd);
			bool flag3 = xyz2 == null;
			Result result;
			if (flag3)
			{
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			else
			{
				List<Curve> list = new List<Curve>();
				list = xlineJig.CreateCurves();
				ptLast = xyz2;
				bool flag4 = list.Count > 0;
				if (flag4)
				{
					line = (list[0] as Line);
				}
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		public Result DrawJigCurveLineYDirection(ref Line line, ref XYZ ptLast, string strPromptStart, string strPromptEnd)
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			bool flag = ptLast != null;
			XYZ xyz;
			if (flag)
			{
				xyz = ptLast;
				xlineJig.StartPoint = xyz;
			}
			else
			{
				xyz = xlineJig.PickStartPoint(strPromptStart);
				bool flag2 = xyz == null;
				if (flag2)
				{
					xlineJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			xlineJig.ResetPoint = XUiJigBase.EResetPoint.eByAngle;
			xlineJig.LineAngle = Math.PI*.5;
			XYZ xyz2 = xlineJig.PickEndPoint(xyz, strPromptEnd);
			bool flag3 = xyz2 == null;
			Result result;
			if (flag3)
			{
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			else
			{
				List<Curve> list = new List<Curve>();
				list = xlineJig.CreateCurves();
				ptLast = xyz2;
				bool flag4 = list.Count > 0;
				if (flag4)
				{
					line = (list[0] as Line);
				}
				xlineJig.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		public Result DrawJigCurveRect(ref List<Curve> lstCurves, string strPromptStart, string strPromptEnd)
		{
			XRectJig xrectJig = new XRectJig(this.m_uiDoc);
			try
			{
				XYZ xyz = xrectJig.PickStartPoint(strPromptStart);
				bool flag = xyz == null;
				if (flag)
				{
					xrectJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				XYZ xyz2 = xrectJig.PickEndPoint(xyz, this.m_uiDoc.ActiveView.RightDirection, strPromptEnd);
				bool flag2 = xyz2 == null;
				if (flag2)
				{
					xrectJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				lstCurves = xrectJig.CreateCurves();
				xrectJig.Dispose();
			}
			catch (Exception)
			{
				xrectJig.Dispose();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DrawJigCurveArc3Pt(ref List<Curve> lstCurves, ref XYZ ptLast, string strPromptStart, string strPromptEnd, string strPromptMid)
		{
			XArc3PtJig xarc3PtJig = new XArc3PtJig(this.m_uiDoc);
			try
			{
				bool flag = ptLast != null;
				XYZ xyz;
				if (flag)
				{
					xyz = ptLast;
					xarc3PtJig.StartPoint = xyz;
				}
				else
				{
					xyz = xarc3PtJig.PickStartPoint(strPromptStart);
					bool flag2 = xyz == null;
					if (flag2)
					{
						xarc3PtJig.Dispose();
						return Autodesk.Revit.UI.Result.Cancelled;
					}
				}
				XYZ xyz2 = xarc3PtJig.PickEndPoint(xyz, strPromptEnd);
				bool flag3 = xyz2 == null;
				if (flag3)
				{
					xarc3PtJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				XYZ xyz3 = xarc3PtJig.PickOnCurvePoint(xyz, xyz2, strPromptMid);
				bool flag4 = xyz3 == null;
				if (flag4)
				{
					xarc3PtJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				lstCurves = xarc3PtJig.CreateCurves();
				ptLast = xyz2;
				xarc3PtJig.Dispose();
			}
			catch (Exception)
			{
				xarc3PtJig.Dispose();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DrawJigCurveArc3Pt(ref Arc arc, ref XYZ ptLast, string strPromptStart, string strPromptEnd, string strPromptMid)
		{
			List<Curve> list = new List<Curve>();
			Result result = this.DrawJigCurveArc3Pt(ref list, ref ptLast, strPromptStart, strPromptEnd, strPromptMid);
			bool flag = list.Count > 0;
			if (flag)
			{
				arc = (list[0] as Arc);
			}
			return result;
		}

		public Result DrawJigCurveArcCenter2Pt(ref List<Curve> lstCurves, string strPromptCenter, string strPromptStart, string strPromptEnd)
		{
			XArcJig xarcJig = new XArcJig(this.m_uiDoc);
			try
			{
				XYZ xyz = xarcJig.PickCenterPoint(strPromptCenter);
				bool flag = xyz == null;
				if (flag)
				{
					xarcJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				XYZ xyz2 = xarcJig.PickStartPoint(xyz, strPromptStart);
				bool flag2 = xyz2 == null;
				if (flag2)
				{
					xarcJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				XYZ xyz3 = xarcJig.PickEndPoint(xyz, xyz2, strPromptEnd);
				bool flag3 = xyz3 == null;
				if (flag3)
				{
					xarcJig.Dispose();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				lstCurves = xarcJig.CreateCurves();
				xarcJig.Dispose();
			}
			catch (Exception)
			{
				xarcJig.Dispose();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DrawJigCurveArcCenter2Pt(ref Arc arc, string strPromptCenter, string strPromptStart, string strPromptEnd)
		{
			List<Curve> list = new List<Curve>();
			Result result = this.DrawJigCurveArcCenter2Pt(ref list, strPromptCenter, strPromptStart, strPromptEnd);
			bool flag = list.Count > 0;
			if (flag)
			{
				arc = (list[0] as Arc);
			}
			return result;
		}

		private UIDocument m_uiDoc;
	}
}
